home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: seebs@solutions.solon.com (Peter Seebach)
- Newsgroups: comp.lang.c
- Subject: Re: Simple Program Question
- Date: 26 Feb 1996 20:05:53 -0600
- Organization: Usenet Fact Police (Undercover)
- Message-ID: <4gtou1$d3q@solutions.solon.com>
- References: <4gsr9u$sk6@newsbf02.news.aol.com> <4gti5g$d78@garden.csc.calpoly.edu>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <4gti5g$d78@garden.csc.calpoly.edu>,
- Dan Stubbs <dstubbs@garden.csc.calpoly.edu> wrote:
- >>long int i, j, k, l;
- >>long int count;
-
- >>int
- >>main (void)
- >>{
- >> do
- >> {
- >> (l = i + j + k);
- >> (i = 1);
- >> (j = 2);
- >> (k = 3);
- >> (i++, j++, k++);
- >> (++count);
- >> if (count % 1000000 == 0)
- >> {
- >> printf("%ld(i) + %ld(j) + %ld(k) = %ld(l)", i, j, k, l);
- >> }
- >>
- >> } while (0 < i < j < k < l);
-
- >> return (0);
- >>}
-
- >It appears to me that you are to fix el (use el instead of l) and then find
- >i, j, and k such that 0<i<j<k and i+j+k = el. Your code doesn't do anything
- >like that. A couple of problems: the first time l = i+j+k is encountered
- >i, j, and k have had no value assigned and hence what happens is undefined.
-
- Bullshit; globals are auto-initialized to zero. :)
-
- >Perhaps that is where your program dies.
-
- More likely, it's that the first time through, count is set to 1, so nothing
- is printed. Then
- 0 < i < j < k < l
- is evaluated, and turns out to be false, because < is a binary op, not a
- normal math relational op, and the program terminates.
-
- Nah. Too easy.
-
- >Then, each time through your do-while loop i,j and k are *all* incremented by
- >exactly 1. Hence the value of i,j and k in ;your program are 1,2,3; 2,3,4;
- >3,4,5; 4,5,6; ... j,j+1,j+2, ... . Surely you want something more general
- >than that.
-
- I would agree; the original program is naive.
-
- >I don't believe your code could compile. Your while condition:
-
- > while (0<i<j<k<l) is not legal. I suspect you want something like:
-
- > while (0<i && i<j && j<k && k<l).
-
- He probably does; however, his code is *legal*. Just stupid.
-
- >Your logic is pretty far off, so here is a solution to the problem as I
- >understand it. (The fixed value of el is read from the command line.)
-
- [Solution snipped. The only immediate problem I saw was that it didn't
- return a value.]
-
- -s
- --
- Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
- C/Unix wizard -- C/Unix questions? Send mail for help. No, really!
- FUCK the communications decency act. Goddamned government. [literally.]
- The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
-